package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

//    @RequiresRoles("ADMIN")
    //权限表达式,可以配置多个,默认是&&,要求同时拥有多个权限才能访问该方法
    @RequiresPermissions(value = {"role:list","角色列表"},logical = Logical.OR)
//    @RequiredPermission(name="员工列表", expression="role:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Role> pageInfo = roleService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "role/list";
    }

//    @RequiresPermissions(value = {"role:xxx","角色xxx"},logical = Logical.OR)
//    @RequestMapping("/list2")
//    public String list2(Model model, QueryObject qo) {
//        PageInfo<Role> pageInfo = roleService.query(qo);
//        model.addAttribute("pageInfo", pageInfo);
//        return "role/list2";
//    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        //查询所有权限
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions", permissions);

        if (id != null) {
            Role role = roleService.select(id);
            model.addAttribute("role", role);
        }
        return "role/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String insertOrUpdate(Role role, Long[] ids) {
        if (role.getId() != null) {
            roleService.update(role, ids);
        } else {
            roleService.insert(role, ids);
        }
//        employeeService.insert(dept);
        return "redirect:/role/list.do";
    }

    @RequiresPermissions("role:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            roleService.delete(id);
        }
        return "redirect:/role/list.do";
    }
}
